# @before-stub-for-debug-begin
from python3problem113 import *
from typing import *
# @before-stub-for-debug-end

#
# @lc app=leetcode.cn id=113 lang=python3
#
# [113] 路径总和 II
#
from typing import List, Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
# @lc code=start
# Definition for a binary tree node.

class Solution:
    def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
        if root is None:
            return []
        self.path = []
        self.all_path = []
        self.preorder(root, targetSum)
        return self.all_path
        
    def preorder(self, root: TreeNode, targetSum: int) -> None:
        targetSum -= root.val
        self.path.append(root.val)
        if targetSum == 0 and root.left is None and root.right is None:
            self.all_path.append(self.path[:])
        if root.left:
            self.preorder(root.left, targetSum)
        if root.right:
            self.preorder(root.right, targetSum)
        self.path.pop()
        
# @lc code=end

